﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace BioinformaticAlgoritms2014
{
   public class MassSpectrometryAnalize
    {

       public MassSpectrometryAnalize()
       {
           massDictionary =
               File.ReadAllLines(Tools.GetFullPath(@"Data\integer_mass_table.txt"))
                   .Select(a => a.Split(' '))
                   .ToDictionary(a => a[0][0], a => int.Parse(a[1]));
       }

       private Dictionary<char, int> massDictionary;

       public static IEnumerable<string> GetSubPeptides(string peptide)
       {
           string cPeptid = peptide + peptide;
           for (int i = 1; i < peptide.Length; i++)
           {
               for (int k = 0; k < peptide.Length; k++)
               {
                   yield return cPeptid.Substring(k, i);
               }
           }
       }

       public IEnumerable<int> GetCyclospectrum(string peptide)
       {
         return new[]{0}.Concat(GetSubPeptides(peptide).Select(a => GetMass(a)).OrderBy(a=>a)).Concat(new []{GetMass(peptide)});
       }

       public int GetMass(string peptide)
       {
           return peptide.ToCharArray().Where(a => massDictionary.ContainsKey(a)).Select(a => massDictionary[a]).Sum();
       }
      
    }
}
